import { defineStore } from 'pinia';
import { del } from "@/utils/request";

export interface State {
    userInfo: {
        mobile: string;
        user: string;
        role?: number
    } | null;
    token: string
}

export const useUserStore = defineStore({
    id: 'user',
    state: ():State => ({
        userInfo: null,
        token: ''
    }),

    actions: {
        // 退出登录
        async doLogout() {
            this.userInfo = null
            this.token = ''
            await del("http://127.0.0.1:3000/files/clear/pictures")
            window.postMessage({type: "CLEAR_CACHE"}, "*")
        }
    },
    persist: {
        enabled: true, //开启存储
        strategies: [
            {
                storage: localStorage, 
                paths: ['userInfo', 'token']
            }
        ]
    }
})